package com.youlu.campus.web.timetable;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.entity.timetable.dto.*;
import com.youlu.campus.service.timetable.UserTimetableConfigService;
import com.youlu.campus.service.timetable.UserTimetableService;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 课程表控制器
 * 处理课程表的增删改查及配置信息管理等操作
 *
 * @author : [dengbo]
 * @createTime : [2022/12/17 16:28]
 */
@Slf4j
@RestController
@RequestMapping("/mini-app/")
public class TimetableController {

    @Autowired
    private UserTimetableService userTimetableService;

    @Autowired
    private UserTimetableConfigService userTimetableConfigService;


    /**
     * 课程保存
     * 保存或更新用户的课程表信息
     * @param request 课程表保存DTO，包含课程详情信息
     * @return 保存结果
     */
    @PostMapping("/timetable/save")
    public QResult createTimetable(@Validated @RequestBody UserTimetableSaveDTO request) {
        String userId = RequestContext.getUserId();
        log.info("userId:{},request param:{}", userId, JSON.toJSONString(request));
        request.setUserId(userId);
        return QResult.success(userTimetableService.save(request));
    }

    /**
     * 课程表查询
     * 根据日期等条件查询用户的课程表信息
     * @param dto 课程表查询DTO，包含查询日期等条件
     * @return 课程表查询结果
     */
    @PostMapping("/timetable/query")
    public QResult queryTimetable(@Validated @RequestBody UserTimetableQueryDTO dto) {
        String userId = RequestContext.getUserId();
        log.info("userId:{},request param:{}", userId, JSON.toJSONString(dto));
        dto.setUserId(userId);
        return QResult.success(userTimetableService.queryTimetableByDate(dto));
    }

    /**
     * 课程表详情查询
     * 根据课程ID查询单个课程的详细信息
     * @param dto 课程详情查询DTO，包含课程ID
     * @return 单个课程的详细信息
     */
    @PostMapping("/timetable/find/one")
    public QResult queryOneTimetable(@Validated @RequestBody UserTimetableQueryOneDTO dto) {
        String userId = RequestContext.getUserId();
        log.info("userId:{},request param:{}", userId, JSON.toJSONString(dto));
        dto.setUserId(userId);
        return QResult.success(userTimetableService.queryTimetableById(dto));
    }

    /**
     * 课程表删除
     * 根据课程ID删除指定的课程信息
     * @param dto 课程删除DTO，包含课程ID
     * @return 删除结果
     */
    @PostMapping("/timetable/delete/one")
    public QResult deleteOneTimetable(@Validated @RequestBody UserTimetableQueryOneDTO dto) {
        String userId = RequestContext.getUserId();
        log.info("userId:{},request param:{}", userId, JSON.toJSONString(dto));
        dto.setUserId(userId);
        return QResult.success(userTimetableService.deleteTimetableById(dto));
    }

    /**
     * 课程表配置信息保存
     * 保存用户的课程表配置信息（如显示设置等）
     * @param dto 课程配置保存DTO，包含配置详情
     * @return 配置保存结果
     */
    @PostMapping("/timetable/config/save")
    public QResult configSave(@Validated @RequestBody UserTimetableConfigCreateDTO dto) {
        String userId = RequestContext.getUserId();
        log.info("userId:{},request param:{}", userId, JSON.toJSONString(dto));
        dto.setUserId(userId);
        return QResult.success(userTimetableConfigService.saveConfig(dto));
    }

    /**
     * 课程表配置信息查询
     * 查询用户的课程表配置信息
     * @param dto 课程配置查询DTO
     * @return 用户的课程表配置信息
     */
    @PostMapping("/timetable/config/query")
    public QResult configQuery(@Validated @RequestBody UserTimetableConfigQueryDTO dto) {
        String userId = RequestContext.getUserId();
        log.info("userId:{},request param:{}", userId, JSON.toJSONString(dto));
        dto.setUserId(userId);
        return QResult.success(userTimetableConfigService.getUserConfig(dto));
    }

}